package com.warrior.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.warrior.entity.TeamEntity;
import com.warrior.entity.UserEntity;
import com.warrior.service.TeamService;
import com.warrior.service.UserService;
import com.warrior.util.CModelAndView;
import com.warrior.util.CommonUtils;
import com.warrior.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by shawn on 2017/4/26.
 */
@Controller
@RequestMapping("/admin")
public class AdminTeamController {

    @Autowired
    private TeamService teamService;

    @Autowired
    private UserService userService;


    /**
     * 队伍列表页
     *
     * @param request   req
     * @param response  resp
     * @param size      大小
     * @param page      当前页
     * @param orderType 排序类型
     * @return ModelAndView
     */
    @RequestMapping("/teamList")
    public ModelAndView listAllTeam(HttpServletRequest request, HttpServletResponse response, String size, String page, String orderType) {
        ModelAndView mv = new CModelAndView("admin/team_list", request, response);
        Page<TeamEntity> teamEntities = this.teamService.findAllTeams(CommonUtils.String2Int(size), CommonUtils.String2Int(page), orderType);
        mv.addObject("teams", teamEntities);
        return mv;
    }


    /**
     * 修改队伍名称
     *
     * @param teamId  队伍名
     * @param newName 新名称
     * @return
     */
    @RequestMapping(value = "/modifiedTeamName", method = RequestMethod.POST)
    @ResponseBody
    public String modifiedTeamName(String teamId, String newName) {
        TeamEntity teamEntity = this.teamService.findTeamByTeamId(teamId);
        teamEntity.setName(newName);
        this.teamService.saveNewTeam(teamEntity);
        String returnMap = null;
        try {
            returnMap = ServerResponse.okToJsonString("success");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return returnMap;

    }

    /**
     * 查看队伍中的用户信息
     *
     * @param request
     * @param response
     * @param teamId
     * @return
     */
    @RequestMapping(value = "review_team", method = RequestMethod.GET)
    public ModelAndView reviewTeam(HttpServletRequest request, HttpServletResponse response, String teamId) {
        ModelAndView mv = new CModelAndView("admin/team_edit", request, response);
        TeamEntity teamEntity = this.teamService.findTeamByTeamId(teamId);
        mv.addObject("team", teamEntity);
        List<UserEntity> userEntities = this.userService.findTeamMember(teamEntity.getCaptionId());
        mv.addObject("users", userEntities);
        return mv;
    }

    /**
     * 修改队伍的审核状态
     *
     * @param teamId 队伍id
     * @param status 需要修改后的状态
     * @return
     */
    @RequestMapping(value = "/review_team", method = RequestMethod.POST)
    @ResponseBody
    public String reviewTeam(String teamId, String status) {
        String returnMap = null;
        try {
            TeamEntity teamEntity = this.teamService.findTeamByTeamId(teamId);
            teamEntity.setTeamStatus(CommonUtils.String2Int(status));
            this.teamService.saveNewTeam(teamEntity);
            returnMap = ServerResponse.okToJsonString("success");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return returnMap;

    }


    /**
     * 拿到所有队伍信息
     *
     * @return Json
     */
    @RequestMapping(value = "/findAllTeams", method = RequestMethod.GET)
    @ResponseBody
    public String findAllTeams() {
        List<TeamEntity> teamEntities = this.teamService.findAllTeamsByListInActivity();
        String returnMap = null;
        try {
            returnMap = ServerResponse.okToJsonString(teamEntities);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return returnMap;
    }


    /**
     * 修改队伍积分页面
     *
     * @param request  req
     * @param response response
     * @return ModelAndView
     */
    @RequestMapping(value = "/modifiedTeamScore", method = RequestMethod.GET)
    public ModelAndView modifiedTeamScore(HttpServletRequest request, HttpServletResponse response,String teamId) {
        ModelAndView mv = new CModelAndView("/admin/team_points", request, response);
        TeamEntity teamEntity = this.teamService.findTeamByTeamId(teamId);
        mv.addObject("team",teamEntity);
        return mv;
    }

    /**
     * 修改队伍积分状况
     *
     * @param teamId       队伍id
     * @param seriesWins   系列赛胜场
     * @param winNum       总胜场
     * @param seriesLose   系列赛负场
     * @param loseNum      总负场
     * @param drawNum      平局数
     * @param teamIntegral 队伍积分
     * @ json
     */
    @RequestMapping(value = "/modifiedTeamScore", method = RequestMethod.POST)
    @ResponseBody
    public String modifiedTeamScore(String teamId, String seriesWins, String winNum, String seriesLose, String loseNum,
                                    String drawNum, String teamIntegral) {
        TeamEntity teamEntity = this.teamService.findTeamByTeamId(teamId);
        teamEntity.setDrawNum(CommonUtils.String2Int(drawNum));
        teamEntity.setSeriesWins(CommonUtils.String2Int(seriesWins));
        teamEntity.setWinNum(CommonUtils.String2Int(winNum));
        teamEntity.setSeriesLose(CommonUtils.String2Int(seriesLose));
        teamEntity.setLoseNum(CommonUtils.String2Int(loseNum));
        teamEntity.setTeamIntegral(CommonUtils.String2Int(teamIntegral));
        this.teamService.saveNewTeam(teamEntity);
        String returnMap = null;
        try {
            returnMap = ServerResponse.okToJsonString("success");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return returnMap;
    }


}
